/*
 *  UserLoginWindow.java 
 *    
 *    Copyright 2012 YAWP Project Team
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.yawp.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Graphics2D;
import javax.swing.JTextArea;
import java.io.IOException;
import java.net.URL;

import javax.swing.UIManager;

/**
 * New User View - allows user to switch users
 * 
 * @author ccato
 *
 */
@SuppressWarnings("serial")
public class UserLoginWindow extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JButton btnLogin;
	private JLabel lblNewLabel;
	private JPanel panel_3;
	private JTextArea txtrWelcomeToYawp;
	private JPanel panel_2;
	private JPanel panel_4;

	/**
	 * Launch the application - for stand alone testing only.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLoginWindow frame = new UserLoginWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Constructor - Create the frame.
	 */
	public UserLoginWindow() {
		setTitle("YAWP - User Login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 250);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	
		
		panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new BorderLayout(0, 0));

		// add the yawp icon
		BufferedImage myPicture = null;
		try {
			URL imageURL = UserLoginWindow.class.getResource("images/yawp logo.jpg");
			myPicture = ImageIO.read(imageURL);
			JLabel picLabel = new JLabel(new ImageIcon( scale(myPicture, 150, 150) ));
			panel_2.add(picLabel, BorderLayout.WEST);
		} catch (IOException e1) {
			System.out.println("Could not find icon file: " + e1.getMessage());
		}
		
		
		panel_4 = new JPanel();
		panel_2.add(panel_4, BorderLayout.EAST);	
		
		txtrWelcomeToYawp = new JTextArea();
		txtrWelcomeToYawp.setBackground(UIManager.getColor("Label.background"));
		txtrWelcomeToYawp.setWrapStyleWord(true);
		txtrWelcomeToYawp.setEditable(false);
		txtrWelcomeToYawp.setLineWrap(true);
		panel_4.add(txtrWelcomeToYawp, BorderLayout.NORTH);

		txtrWelcomeToYawp.setText("Welcome to YAWP.  Please enter your user name.");
		
		panel_3 = new JPanel();
		contentPane.add(panel_3, BorderLayout.SOUTH);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_3.add(panel);
		
		lblNewLabel = new JLabel("User Name:");
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent evt) {
				int key = evt.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					btnLogin.doClick();
				}
			}
		} );
		
		panel.add(textField);
		textField.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_3.add(panel_1, BorderLayout.SOUTH);
		
		btnLogin = new JButton("Log In");
		panel_1.add(btnLogin);

		// set focus to the text field
		addWindowFocusListener(new WindowAdapter() {
		    public void windowGainedFocus(WindowEvent e) {
		        textField.requestFocusInWindow();
		    }
		} );

	}

	/**
	 *  hook for view to register listener for the login button
	 * @param listener
	 */
	public void addUserLoginListener(ActionListener listener) {
		btnLogin.addActionListener(listener);
	}

	/**
	 *  support function to get user input by the view
	 * @return User
	 */
	public String getUserText() {
		return textField.getText();
	}
	
	/**
	 * Scale a buffered image
	 * copied forum example code from: http://www.coderanch.com/t/346438/GUI/java/Adjusting-image-size-image-label
	 * 
	 * Used to scale the yawp logo
	 * 
	 * @param src image to scale
	 * @param w width
	 * @param h hight
	 */
	private BufferedImage scale(BufferedImage src, int w, int h) {  
        int type = BufferedImage.TYPE_INT_RGB;  
        BufferedImage dst = new BufferedImage(w, h, type);  
        Graphics2D g2 = dst.createGraphics();  
        // Fill background for scale to fit.  
        g2.setBackground(UIManager.getColor("Panel.background"));  
        g2.clearRect(0,0,w,h);  
        double xScale = (double)w/src.getWidth();  
        double yScale = (double)h/src.getHeight();  
        // Scaling options:  
        // Scale to fit - image just fits in label.  
        double scale = Math.min(xScale, yScale);  
        // Scale to fill - image just fills label.  
        //double scale = Math.max(xScale, yScale);  
        int width  = (int)(scale*src.getWidth());  
        int height = (int)(scale*src.getHeight());  
        int x = (w - width)/2;  
        int y = (h - height)/2;  
        g2.drawImage(src, x, y, width, height, null);  
        g2.dispose();  
        return dst;  
    }  

}
